﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Base.Helper;

namespace Framework.Library.Base.UI
{
    public class Menu : Control
    {
        public ConfigHelper cfg = new ConfigHelper();
        public string menu { get; set; }
        public string menuLink { get; set; }
        public string menuSub { get; set; }
        public int parentMenu { get; set; }
        public List<PortalMenu> DataSource { get; set; }        

        protected override void Render(HtmlTextWriter writer)
        {
            //GetCurrentItem();
            if (DataSource == null)
                DataSource = new List<PortalMenu>();
            string menuClass = string.IsNullOrEmpty(menu) ? string.Empty : ("class=\"" + menu + "\"");
            writer.WriteLine("<ul " + menuClass + " id=\"menu_" + parentMenu + "\">");

            writer.Write(createMenu());
            writer.WriteLine("</ul>");
        }

        private string createMenu()
        {
            var _dtMenus = new List<PortalMenu>();
            _dtMenus = DataSource.FindAll(l => (l.PARENTID == parentMenu && l.VISIBLE > 0 && l.ENABLE > 0));
            var sb = new StringBuilder();
            int index = 0;
            foreach (PortalMenu portalMenu in _dtMenus)
            {
                //string curClass = isActiveMenu(portalMenu.ID) ? "menucurrent" : menuLink;
                string curClass = "";
                if (index != 0)
                    sb.Append("<li><div class='separator'></div></li>");
                sb.Append("<li>");
                switch (portalMenu.LINKTYPE)
                {
                    case 0:
                        sb.Append(createHeader(curClass, portalMenu.NAME));
                        break;
                    case 1:
                        sb.Append(createLink(curClass, portalMenu.NAME, cfg.MenuPath(portalMenu.PORTALID, portalMenu.ID, portalMenu.PARAMETTERS.Trim())));
                        break;
                    case 2:
                        sb.Append(createLink(portalMenu.NAME, portalMenu.PARAMETTERS.Trim()));
                        break;
                    case 3:
                        sb.Append(createButton(curClass, portalMenu.NAME, portalMenu.PARAMETTERS.Trim()));
                        break;
                    default:
                        break;
                }
                sb.Append(LoadMenus(DataSource, 0, portalMenu.ID));
                sb.Append("</li>");
                index++;
            }
            return sb.ToString();
        }

        private string LoadMenus(List<PortalMenu> dtCommands, int level, int id)
        {
            var _dtMenus = new List<PortalMenu>();
            _dtMenus = dtCommands.FindAll(l => (l.PARENTID == id && l.VISIBLE > 0 && l.ENABLE > 0));
            var sb = new StringBuilder();
            if (_dtMenus.Count > 0)
            {
                level++;
                sb.Append("<ul>");
                foreach (PortalMenu portalMenu in _dtMenus)
                {
                    sb.Append("<li>");
                    switch (portalMenu.LINKTYPE)
                    {
                        case 0:
                            sb.Append(createHeader(menuSub, portalMenu.NAME));
                            break;
                        case 1:
                            sb.Append(createLink(portalMenu.NAME, cfg.MenuPath(portalMenu.PORTALID, portalMenu.ID, portalMenu.PARAMETTERS.Trim())));
                            break;
                        case 2:
                            sb.Append(createLink(portalMenu.NAME, portalMenu.PARAMETTERS.Trim()));
                            break;
                        case 3:
                            sb.Append(createButton(portalMenu.NAME, portalMenu.PARAMETTERS.Trim()));
                            break;
                        default:
                            break;
                    }
                    sb.Append(LoadMenus(DataSource, level, portalMenu.ID));
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }
            return sb.ToString();
        }

        private string createHeader(string className, string headerText)
        {
            return string.Format("<a {0}>{1}</a>",
                                 string.IsNullOrEmpty(className) ? string.Empty : "class='" + className + "'",
                                 headerText);
        }

        private string createLink(string className, string linkText, string linkURL)
        {
            return string.Format("<a href='{0}' {2}>{1}</a>", linkURL, linkText,
                                 string.IsNullOrEmpty(className) ? string.Empty : "class='" + className + "'");
        }

        private string createLink(string linkText, string linkURL)
        {
            return string.Format("<a href='{0}'>{1}</a>", linkURL, linkText);
        }

        private string createButton(string className, string linkText, string linkAction)
        {
            return string.Format("<a href='javascript:;' onclick='{0}' {2}>{1}</a>", linkAction, linkText,
                                 string.IsNullOrEmpty(className) ? string.Empty : "class='" + className + "'");
        }

        private string createButton(string linkText, string linkAction)
        {
            return string.Format("<a href='javascript:;' onclick=\"{0}\">{1}</a>", linkAction, linkText);
        }

        private bool isActiveMenu(int _menu)
        {
            int _curmenu = Convert.ToInt32(HttpContext.Current.Request.QueryString["m"]);
            bool _active = false;
            var list = new ArrayList();
            if (_curmenu > 0)
            {
                list.Add(_curmenu);
                int _parent = _curmenu;
                while (_parent != 0)
                {
                    var _dtMenus = new List<PortalMenu>();
                    _dtMenus = DataSource.FindAll(l => (l.ID == _parent && l.VISIBLE > 0 && l.ENABLE > 0));
                    if (_dtMenus.Count > 0)
                    {
                        _parent = Convert.ToInt32(_dtMenus[0].PARENTID);
                        if (_parent != 0)
                            list.Add(_parent);
                    }
                    else
                    {
                        _parent = 0;
                    }
                }
            }
            if (_menu == _curmenu)
                _active = true;
            else
            {
                if (list.Count > 1)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (Convert.ToInt32(list[i]) == _menu)
                        {
                            _active = true;
                            break;
                        }
                    }
                }
            }
            return _active;
        }

        //private void GetCurrentItem()
        //{
        //    if (String.IsNullOrEmpty(menu))
        //    {
        //        menu = "menu";
        //    }
        //    else if (String.IsNullOrEmpty(menuLink))
        //    {
        //        menuLink = "menulink";
        //    }
        //    else if (String.IsNullOrEmpty(menuSub))
        //    {
        //        menuSub = "sub";
        //    }
        //}
    }
}